<?php
require_once 'Class/BaseClass.php';
require_once 'Class/Drugs.php';
require_once 'Modules/BaseModule.php';
require_once 'Core/Session.php';

/*summary药物*/
class M_Drugs extends ModuleBase{
	
	
	/**
	 *
	 * @param Drugs $Drugs
	 * @return NULL|Drugs
	 */
	public function single($Drugs=null){
		if($Drugs===null ||!isset($Drugs->Id) || empty($Drugs->Id)){
			return null;
		}
		$o=$this->_dishread('Drugs', $Drugs->Id);
		if($o===null){
			foreach($this->db->select('Drugs',array('Id'=>$Drugs->Id)) as $i){
				$o=new Drugs($i);
			}
			if($o!==null)
        		$this->_dishsave('Drugs', $o->Id, $o);
		}
		return $o;
	}
	
	/**
	 * 
	 * @param Drugs $Drugs
	 */
	public function delete($Drugs=NULL){
		if($Drugs===null ||!isset($Drugs->Id) || empty($Drugs->Id)){
			return 0;
		}
		$o=$this->single($Drugs);
		$o->Status=0;
		$result=$this->update($o);
		if($result>0){
			$this->_dishsave('Drugs', $o->Id, $o);
			return $result;
		}else{
			return 0;
		}
	}
	
	/**
	 *
	 * @param Drugs $Drugs
	 * @return Drugs
	 */
	public function insert($Drugs=null){
		$o=new Drugs($Drugs);
		$o->Id=$this->db->guid();
		$o->CreateDate=time();
		$o->ModifyDate=time();
		if($this->db->insert('Drugs',$o->__modetoarray())>0){
			$this->_dishsave('Drugs', $o->Id, $o);
		}
		else{
			$o->Id=null;
		}
		return $o;
	}
	
	/**
	 *
	 * @param Drugs $Drugs
	 * @return int
	 */
	public function update($Drugs=null){
		if($Drugs===null ||!isset($Drugs->Id) || empty($Drugs->Id)){
			return null;
		}
		$Drugs=new Drugs($Drugs);
		$Drugs->ModifyDate=time();
		
		if($this->db->update('Drugs',$Drugs->__modetoarray(array('Id','CreateDate')),array('Id'=>$Drugs->Id))>0){
			$this->_dishsave('Drugs', $Drugs->Id, $Drugs);
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 *
	 * @param $Id;$Name;$Status;$MinCreate;$MaxCreate;
	 * @return array() Drugs;
	 */
	public function arraylist($search=null){
	
		$li=array();
		$cond=array();
		if(isset($search->Id) && !empty($search->Id)){
			$cond['Id']=$search->Id;
		}
		if(isset($search->Name) && !empty($search->Name)){
			$cond['Name']=$search->Name;
		}
		if(isset($search->Status) && is_numeric($search->Status)){
			$cond['Status']=$search->Status;
		}
		if(isset($search->MinCreate) && is_numeric($search->MinCreate)){
			$cond[]=array('CreateDate','>=',$search->MinCreate);
		}
		if(isset($search->MaxCreate) && is_numeric($search->MaxCreate)){
			$cond[]=array('CreateDate','<=',$search->MaxCreate);
		}
		$PageIndex=0;$PageSize=20;
		if($search!==null){
			if(is_numeric($search->PageIndex)){
				$PageIndex=$search->PageIndex;
			}
			if(is_numeric($search->PageSize)){
				$PageSize=$search->PageSize;
			}
		}
		foreach($this->db->select('Drugs',$cond,'[Order] desc',array(intval($PageIndex),intval($PageSize))) as $i){
			$o=new Drugs($i);
			$li[]=$o;
		}
		return $li;
	}


}?>